package com.zhuang.chapter11.work02;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Scanner;

public class MytoBinaryString {

	public static void main(String[] args) {
		System.out.println("输入一个数，转为二进制字符串！");
		Scanner scanner = new Scanner(System.in);

		String binaryString = toBinaryString(scanner.nextInt());

		System.out.println(binaryString);
	}

	/**
	 * 
	 * @Title: toBinaryString
	 * @Description: 数字转为二进制
	 * @param: @param n
	 * @author: KangXiaoZhuang
	 * @email: ITkxz0830@163.com
	 */
	public static String toBinaryString(int n) {
		int mod = 0;
		// 创建栈来存储余数
		Deque<Integer> stack = new ArrayDeque<>();
		do {
			// 获取余数
			mod = n % 2;
			// stack.push(mod);
			stack.addFirst(mod);
			// 获取商
			n = n / 2;
		} while (n > 0);
		return stack.toString();
	}
}
